import BaseListItem from '../base/BaseListItem.js';
import Common from "../../common/common.js";
import TimNet from '../../timnet.js';
import Talk from './Talk.js';

function ConversationListItem (data) {
    BaseListItem.call(this,{});
    this.element.unreadCount = Common.tool.createElement({label: "span",props:{className: "unreadCount"}});
    this.element.appendChild(this.element.unreadCount);
    this.setData(data)
}
let prototype = Object.create(BaseListItem.prototype);
prototype.constructor = ConversationListItem;
ConversationListItem.prototype  = prototype;

//操作
ConversationListItem.prototype.initHandleData = function () {
    return [
        {label:"span",tag:"del",props:{className:"del"}}
    ];
}

//各个操作事件
ConversationListItem.prototype.bind = function(){
    Common.tool.addHandle(this.element.handle.del,"click",()=>{
        if(window.confirm("确定要删除此会话?")){
            TimNet.deleteConversation(this.data.conversationID);
        }
    });

    Common.tool.addHandle(this.element,"click",()=>{
        //打开会话
        Talk.open(this.data);
    });
}

//设置未读
ConversationListItem.prototype.setUnreadCount = function(unreadCount){
    if(unreadCount == 0){
        this.element.unreadCount.innerText = "";
        this.element.removeChild(this.element.unreadCount)
    }else{
        this.element.unreadCount.innerText = (unreadCount>99?"99+":unreadCount);
    }

}

//设置数据
ConversationListItem.prototype.setData = function(data){
    this.data = data;
    switch (data.type) {
        case TIM.TYPES.CONV_GROUP:
            this.setAvatar(Common.tool.getAvatar(data.groupProfile.avatar));
            this.setName(data.groupProfile.name);
            break;
        case TIM.TYPES.CONV_C2C:
            this.setAvatar(Common.tool.getAvatar(data.userProfile.avatar));
            this.setName(Common.tool.isNull(data.userProfile.nick)?data.userProfile.userID:data.userProfile.nick);
            break;
        default:
            this.setAvatar(Common.tool.getAvatar("system"));
            this.setName("系统通知");
                break;
    }
    this.setUnreadCount(data.unreadCount);
    this.setOther(Common.tool.getMessTime(data.lastMessage.lastTime));
    this.setMsg(data.lastMessage.messageForShow);
}





export default ConversationListItem
